/*  This file is part of Basepairs, an application for calculations on
    DNA Sequence polymorphism.
    Copyright (C) 2013 Kim Lindgren

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/


package basepairs;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class loadFile {
    public static int readFile(String file){
        int r = 0;
        
        if(file.substring(file.length()-3, file.length()).toLowerCase().equals("fas")){
            r = readFasta(file);
        }
        else if(file.substring(file.length()-5, file.length()).toLowerCase().equals("fasta")){
            r = readFasta(file);
        }
        
        return r;
    }
    
    static int readFasta(String file){
        Sequences.sequences = new ArrayList<String>();
        
        try{
            BufferedReader fFile = new BufferedReader(new FileReader(file));
            String line = fFile.readLine();
            
            int currSequence = -1;
            
            while(line != null){
                
                if(line.substring(0,1).equals(">")){
                    Sequences.names.add(line.split(" ")[0].substring(1));
                    Sequences.sequences.add("");
                    currSequence++;
                }
                else{
                    Sequences.sequences.set(currSequence, Sequences.sequences.get(currSequence) + line);
                }
                line = fFile.readLine();
            }
            
            fFile.close(); 
        } catch (IOException e) {
            System.out.println("Can not read file.");
            return 404;
        }
        
        return 0;
    }
}
